<?php
App::uses('AuthComponent', 'Controller/Component');
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 * @property Location $Location
 * @property Product $Product
 */
class User extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'username';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		
		'username' => array(
                        'unique' => array(
                       'rule' => 'isUnique',
                       'message' => 'Username is already exists'
                         ),
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'you cannot leave empty here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'minLength' => array(
				'rule' => array('minLength',4),
				'message' => 'at least 4 characters',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
                    'characters' => array(
                        'rule'     => 'alphaNumeric',
                        'message'  => 'Alphanumeric characters only'
                    ),
                   
		),
		'password' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'can not leave empty',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'minLength' => array(
				'rule' => array('minLength',6),
				'message' => 'at least 6 characters',
				//'allowEmpty' => false, 
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
                'confirm' => array(
                            array(
                            'rule' => 'notEmpty',
                            'required' => true,
                            'message' => 'Please Enter Confirm password'
                            ),
                            array(
                                    'rule' => 'checkpasswords',
                                    'required' => true,
                                    'message' => 'Password & Confirm Password must be match.'
                            )
                    ),
		'email' => array(
                    'unique' => array(
                    'rule' => 'isUnique',
                    'message' => 'Email is already exists'
                ),
			'email' => array(
				'rule' => array('email'),
                                
				'message' => 'invalide email',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
                        
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				'message' => 'not allow empty',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
                     
                  
		),
		'phone' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		
	);
        function checkpasswords()     // to check pasword and confirm password
        {  
            if(strcmp($this->data['User']['password'],$this->data['User']['confirm']) == 0 ) 
            {
                return true;
            }
            return false;
        }

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Location' => array(
			'className' => 'Location',
			'foreignKey' => 'location_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Product' => array(
			'className' => 'Product',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	/**
 * hasMany associations
 *
 * @var array
 */
public function beforeSave($options = array()) {
		if (isset($this->data[$this->alias]['password'])) {
			$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		}
		return true;
	}

}
